package canal.client;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.util.List;

public class Mysql2Redis {

    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/order?useSSL=false";
        String username = "oc";
        String password = "!QAZ2wsx";
        PooledDataSource dataSource = new PooledDataSource(driver, url, username, password);
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(ItemsMapper.class);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            ItemsMapper mapper = session.getMapper(ItemsMapper.class);
//            Items items = mapper.selectItems(48706);
//            System.out.println(items);
            List<Items> items = mapper.selectAllItems();
            for (Items item : items) {
                RedisUtil.stringSet("user:" + item.getId(), JSONObject.toJSONString(item));
                System.out.println(item);
            }
        }
    }
}
